import axios from 'axios'
import { message } from 'antd';

export default class Http {
  static request(options) {
    let baseUrl = '/api';
    // let baseUrl = 'http://172.30.20.192';
    let headers = options.headers || {};
    if (window.localStorage.getItem('userInfo')) {
        headers = Object.assign(headers, {
          token: JSON.parse(window.localStorage.getItem('userInfo')).token || ''
        });
    }
    
    return new Promise((resolve, reject) => {
      axios({
        url: options.url,
        baseURL: baseUrl,
        method: options.method || "get",
        params: options.params || "",
        data: options.data || "",
        headers: headers || "",
        timeout: 50000
      }).then((response) => {
        if (response.status === 200) {
          if (response.data.code === 40000) {
            resolve(response.data)
          } else {
            message.error(response.data.message);
          }
        } else {
          reject(response.data)
        }
      }).catch(err => {
        message.error(err.response.data.message);
      })
    })
  }
}